﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Symbonix.NorSoNet.Security.DAL.Repositories;
using Symbonix.NorSoNet.Framework;

public partial class activate_account : PublicBasePage
{
    private string ActivationKey
    {
        get
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["key"]))
                    return Request.QueryString["key"].ToString();
                else
                    return string.Empty;
            }
            catch
            {
                return string.Empty;
            }
        }
    }


    private string Email
    {
        get
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["email"]))
                    return Request.QueryString["email"].ToString();
                else
                    return string.Empty;
            }
            catch
            {
                return string.Empty;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ActivateUser();
            ((MasterPages_Public)this.Master).ShowLoginInLink(CustomSiteMap.GetUrl(CustomSiteMap.Name.Login.ToString()), "Login");
        }
    }

    private void ActivateUser()
    {
        if (Email != null)
        {
            ExtendedUserRepository eur = new ExtendedUserRepository();
            Symbonix.Library.Web.Security.DAL.Entities.User user = eur.GetUserNameByEmail(Email);
            if (user != null)
            {
                string key=Symbonix.Library.Utility.Security.Decrypt(ActivationKey, user.PasswordSalt, true);
                if (key == user.UserName)
                {
                    user.IsApproved = true;
                    eur.Update(user);
                    pnlSuccess.Visible = true;
                    pnlfail.Visible = false;
                }
                else
                {
                    pnlSuccess.Visible = false;
                    pnlfail.Visible = true;
                }
            }
        }
    }
}